package com.app.view.component 
{
	import flash.events.MouseEvent;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;	

	import com.app.view.manager.LayerManagerMediator;
	import com.app.view.manager.SceneManagerMediator;
	import com.app.view.manager.DialogManagerMediator;
	import com.app.view.manager.SoundManagerMediator;
	
	import com.app.view.component.RoomMediator;
	import com.app.view.component.scene.HallScene;
	import com.peachcastle.events.HallEvent;
	
	import com.peachcastle.debug.Log;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class HallMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "HallMediator";
		public static const ADD_IN_LAYER:String = NAME + "_AddInLayer";
		public static const SHOW_ROOM_LIST:String = NAME + "_ShowRoomList";
		public static const UPDATE_ROOM:String = NAME + "_UpdateRoom";
		public static const HIDE_CRETAE_ROOM:String = NAME + "_HideCreateRoom";
		
		public static const START_GAME_REQUEST:String = NAME + "_StartGameRequest";
		public static const START_GAME_RESPONSE:String = NAME + "_StartGameResponse";
		
		private var _hallSence:HallScene = null;
		
		private var _selectRoomEvent:HallEvent = null;
		
		public function HallMediator(hallSence:HallScene) 
		{
			super(NAME);
			
			_hallSence = hallSence;
			_hallSence.setMediator(this);
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				HallMediator.ADD_IN_LAYER,
				HallMediator.SHOW_ROOM_LIST,
				HallMediator.HIDE_CRETAE_ROOM,
				HallMediator.UPDATE_ROOM,
				
				LayerManagerMediator.GET_LAYERS,
				SceneManagerMediator.CURRENT_SCENE
			];
		}	
		
		override public function handleNotification(notification:INotification):void
		{
			var name:String = notification.getName();
			var param:* = notification.getBody();
			switch ( name )
			{
				case HallMediator.ADD_IN_LAYER:
					_hallSence.setUserData(param[0], param[1]);
					_hallSence.addLayer();
					sendNotification(SceneManagerMediator.LEAVE_PRE_SCENE);
					sendNotification(LayerManagerMediator.ADD_IN_LAYER, [LayerManagerMediator.HALL_LAYER, _hallSence.assest]);
					sendNotification(SceneManagerMediator.PRE_LOAD, ["RoomScene"]);

					break;
				case HallMediator.SHOW_ROOM_LIST:
					_hallSence.halllist.serverUpdate(param[0]);
			
					break;
					
				case HallMediator.HIDE_CRETAE_ROOM:
					sendNotification(DialogManagerMediator.HIDE_DIALOG, ['createRoom']);
					if (param[0]) //是否是庄家
					{
						sendNotification(RoomMediator.INIT_ROOM_REQUEST, [param[1], param[2]]);
					}
			
					break;
					
				case HallMediator.UPDATE_ROOM:
					_hallSence.halllist.serverUpdateRoom(param[0]);
					
					break;
					
				/**
				 * 表现层监听
				 */
				case LayerManagerMediator.GET_LAYERS:
					_hallSence.toolbar.layers = param[0];
					
					break;
					
				case SceneManagerMediator.CURRENT_SCENE:
					_hallSence.toolbar.sence = param[0];
					
					break;
			}
		}
		
		public function onCreateRoom(e:MouseEvent):void
		{
			sendNotification(DialogManagerMediator.SHOW_DIALOG, ['createRoom', _hallSence.userData]);
		}
		
		public function onStartGame(e:MouseEvent):void
		{
			sendNotification(HallMediator.START_GAME_REQUEST);
		}	
		
		public function onHallSelectRoom(event:HallEvent):void
		{
			_selectRoomEvent = event;
		}
		
		public function onJoinRoom(e:MouseEvent):void
		{
			if (_selectRoomEvent)
			{
				sendNotification(RoomMediator.INIT_ROOM_REQUEST, [_selectRoomEvent.data.sid, _selectRoomEvent.data.roomid]);
			}
		}
		
		public function onHallSelectItem(event:HallEvent):void
		{
			sendNotification(RoomMediator.INIT_ROOM_REQUEST, [event.data.sid, event.data.roomid]);
		}
		
	}

}